package eworldpacs.ui;

import java.util.List;
import java.util.Stack;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class LeadTreeView {
   
   private class NodeLayoutPair{
      private LeadTreeNode _node;
      private ViewGroup _layout;
      
      @SuppressWarnings("unused")
      private NodeLayoutPair(){}
      
      public NodeLayoutPair(LeadTreeNode node, ViewGroup layout){
         _node = node;
         _layout = layout;
      }
      
      public LeadTreeNode getNode(){
         return _node;
      }
      
      public ViewGroup getLayout(){
         return _layout;
      }
   }
   
   private Context _context;
   private ILeadTreeNodeClickedDelegate _treeNodeClickedDelegate;
   private ScrollView _verticalScrollView;
   private HorizontalScrollView _horizontalScrollView;
   private LinearLayout _rootTreeViewLinearLayout;
   private OnClickListener _rowClickListener;
   private LinearLayout _selectedRow;
   private LeadTreeNode _selectedNode;
   private boolean _processingClick = false;
   
   public LeadTreeView(Context context, ILeadTreeNodeClickedDelegate valueRetriever){
      _context = context;
      _treeNodeClickedDelegate = valueRetriever;
      _selectedNode = null;
      _rootTreeViewLinearLayout = new LinearLayout(_context);
      _rootTreeViewLinearLayout.setOrientation(LinearLayout.VERTICAL);
      _verticalScrollView = new ScrollView(_context);
      _horizontalScrollView = new HorizontalScrollView(_context);
      ViewGroup.LayoutParams verticalScrollViewLayoutParams = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
      ViewGroup.LayoutParams horizontalScrollViewLayoutParams = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
      _verticalScrollView.setLayoutParams(verticalScrollViewLayoutParams);
      _horizontalScrollView.setLayoutParams(horizontalScrollViewLayoutParams);
      
      _horizontalScrollView.addView(_rootTreeViewLinearLayout);
      _verticalScrollView.addView(_horizontalScrollView);
      
      _rowClickListener = new OnClickListener(){

         @Override
         public void onClick(View v) {
               if (!_processingClick){
                  _processingClick = true;
                  LinearLayout rowLayout = (LinearLayout) v;
                  View nodeView = rowLayout.getChildAt(1);
                  _selectedNode = (LeadTreeNode)nodeView.getTag();
                  _treeNodeClickedDelegate.onLeadTreeNodeClicked(_selectedNode);
                  if (rowLayout != _selectedRow && _selectedRow != null){
                     _selectedRow.setBackgroundColor(Color.TRANSPARENT);
                  }
                  _selectedRow = rowLayout;
                  _selectedRow.setBackgroundColor(Color.rgb(25, 25, 112));
                  
                  ViewGroup nodeLinearLayout = (ViewGroup)rowLayout.getParent();
                  
                  //childGroup present
                  if (nodeLinearLayout.getChildCount() > 1){
                     TextView collapseSwitchTextView = (TextView)rowLayout.getChildAt(0);
                     View subView = nodeLinearLayout.getChildAt(1);
                     if (subView.getVisibility() == View.VISIBLE){
                        collapseSwitchTextView.setText("+ ");
                        subView.setVisibility(View.GONE);
                     } else {
                        collapseSwitchTextView.setText("- ");
                        subView.setVisibility(View.VISIBLE);
                     }
                  }
                  _processingClick = false;
               }
            }
         };
   }
   
   public void setRootLevelNode(LeadTreeNode rootNode){
      expandTree(rootNode, _rootTreeViewLinearLayout, true);
   }
   
   private void expandTree(LeadTreeNode rootNode, ViewGroup parentLayout, boolean firstCall){
      LinearLayout childGroup = new LinearLayout(_context);
      childGroup.setOrientation(LinearLayout.VERTICAL);
      LeadTreeNode currentNode = rootNode;
      Stack<NodeLayoutPair> nextElementStack = new Stack<NodeLayoutPair>();
      while (currentNode != null){
         if (currentNode.hasChildren()){
            List<LeadTreeNode> childNodes = currentNode.getChildren();
            for (LeadTreeNode node : childNodes){
               LinearLayout nodeLinearLayout = new LinearLayout(_context);
               nodeLinearLayout.setOrientation(LinearLayout.VERTICAL);
               LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
               if (!firstCall)
                  layoutParams.leftMargin = 15;
               
               LinearLayout rowLayout = new LinearLayout(_context);
               rowLayout.setOrientation(LinearLayout.HORIZONTAL);
               rowLayout.setOnClickListener(_rowClickListener);
               TextView collapseSwitchTextView = new TextView(_context);
               collapseSwitchTextView.setTextColor(Color.WHITE);
               collapseSwitchTextView.setTextSize(24);
               collapseSwitchTextView.setTypeface(Typeface.MONOSPACE);
               if (node.hasChildren()){
                  collapseSwitchTextView.setText("+ ");
               } else {
                  collapseSwitchTextView.setText("  ");
               }
               rowLayout.addView(collapseSwitchTextView);
               rowLayout.addView(node.getView());		
               
               nodeLinearLayout.addView(rowLayout, layoutParams);
               childGroup.addView(nodeLinearLayout, layoutParams);
               
               if (node.hasChildren()){
                  nextElementStack.push(new NodeLayoutPair(node, nodeLinearLayout));
               }
            }
            LinearLayout.LayoutParams childGroupLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            if (firstCall){
               firstCall = false;
            }
            else{
               childGroup.setVisibility(View.GONE);
            }
            parentLayout.addView(childGroup, childGroupLayoutParams);
         }
         if (nextElementStack.size() > 0){
            childGroup = new LinearLayout(_context);
            childGroup.setOrientation(LinearLayout.VERTICAL);
            NodeLayoutPair nodeLayoutPair = nextElementStack.pop();
            currentNode = nodeLayoutPair.getNode();
            parentLayout = nodeLayoutPair.getLayout();
         }
         else
         {
            currentNode = null;
         }
         
      }
   }
   
   public View getView(){
      return _verticalScrollView;
   }
   
   public LeadTreeNode getSelectedNode(){
      return _selectedNode;
   }
}
